package com.example.demo.util;

import com.example.demo.dao.DishesDao;
import com.example.demo.model.Dishes;
import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.corpus.tag.Nature;
import com.hankcs.hanlp.seg.common.Term;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class hanLP {
    private final DishesDao dishesDao;

    public hanLP(DishesDao dishesDao){
        this.dishesDao = dishesDao;
    }
    public static List<Dishes> Search(String description,DishesDao dishesDao) {
//        // 用户输入的描述内容
//        String description = "我喜欢吃猪肉";  // 替换成用户实际的输入
        // 使用HanLP进行分词和词性标注处理
        List<Term> termList = HanLP.segment(description);

        // 遍历分词和词性标注结果
        List<String> keywords = new ArrayList<>();
        for (Term term : termList) {
            System.out.println(term.word);
            Nature nature = term.nature;  // 获取词性
            if (nature.toString().startsWith("n") || nature.toString().startsWith("v")) {  // 仅保留名词和动词
                keywords.add(term.word);
            }
        }

        List<Dishes> dishes = new ArrayList<>();
        dishes = dishesDao.findAll();
        List<String> dishName = dishes.stream()
                .map(Dishes::getName) // 这里假设 Dishes 类中有一个 getName 方法来获取菜品名字
                .collect(Collectors.toList());

        // 根据相似度推荐菜品
        List<String> recommendations = new ArrayList<>();

        for ( String dish : dishName) {
            double maxSimilarity = 0.0;
            for (String keyword : keywords) {
                double similarity = calculateSimilarity(dish, keyword);
                System.out.println(similarity);
                if (similarity > maxSimilarity) {
                    maxSimilarity = similarity;
                }
            }
            if (maxSimilarity > 0.4) {  // 设置一个相似度阈值
                recommendations.add(dish);
            }
        }

        List<Dishes> dishes1 = new ArrayList<>();
//        通过recommendations链表里面的菜品的名字查询菜品并将菜品添加到dishes1里面
        for (String recommendation : recommendations) {
            Dishes recommendedDish = dishesDao.findByName(recommendation); // 假设这里使用 dishesRepository 根据菜品名字查找菜品
            if (recommendedDish != null) {
                dishes1.add(recommendedDish);
            }
        }
        return dishes1;
    }

    private static double calculateSimilarity(String s1, String s2) {
        String[] words1 = s1.split("");
        String[] words2 = s2.split("");

        // 构建词频向量
        Map<String, Integer> vector1 = new HashMap<>();
        Map<String, Integer> vector2 = new HashMap<>();

        for (String word : words1) {
            vector1.put(word, vector1.getOrDefault(word, 0) + 1);
        }

        for (String word : words2) {
            vector2.put(word, vector2.getOrDefault(word, 0) + 1);
        }

        // 计算余弦相似度
        double dotProduct = 0.0;
        double normVector1 = 0.0;
        double normVector2 = 0.0;

        for (String word : vector1.keySet()) {
            dotProduct += vector1.get(word) * vector2.getOrDefault(word, 0);
            normVector1 += Math.pow(vector1.get(word), 2);
        }

        for (String word : vector2.keySet()) {
            normVector2 += Math.pow(vector2.get(word), 2);
        }

        if (normVector1 == 0 || normVector2 == 0) {
            return 0.0;  // 避免除以零
        } else {
            return dotProduct / (Math.sqrt(normVector1) * Math.sqrt(normVector2));
        }
    }
}

